﻿/********
* Copyright © 深圳市新元素医疗技术开发有限公司 . All rights reserved.
* 
*  Description:
*  Create By Kain at 5/6/2011 1:47:00 PM
*  
*
*  Revision History:
*  Date                  Who                 What
*  
* 
*/

#region namespace

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

#endregion

namespace Cherry.Agile.Data.DataAccess
{
    /// <summary>
    /// 
    /// </summary>
    public class DefaultInstrumentationProvider : IInstrumentationProvider
    {

        const string CATEGORY_NAME = "Remp Data";
        const string NAME = "Remp Data Connection Performance Counter";

        private PerformanceCounter _connectedCounter;
        private PerformanceCounter _disconnetedCounter;
        private PerformanceCounter _connectionCounter;

        static DefaultInstrumentationProvider()
        {
            SetupCategory();
        }

        public static void SetupCategory()
        {
            if (!PerformanceCounterCategory.Exists(CATEGORY_NAME))
            {

                CounterCreationDataCollection CCDC = new CounterCreationDataCollection();

                CounterCreationData a = new CounterCreationData();
                a.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
                a.CounterName = "Connection Created/sec";
                CCDC.Add(a);

                CounterCreationData b = new CounterCreationData();
                b.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
                b.CounterName = "Connection Disposed/sec";
                CCDC.Add(b);

                CounterCreationData c = new CounterCreationData();
                c.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
                c.CounterName = "Connection Actived";
                CCDC.Add(c);

                PerformanceCounterCategory.Create(CATEGORY_NAME, NAME, PerformanceCounterCategoryType.MultiInstance, CCDC);

            }
        }

        public static void RemoveCategory()
        {
            if (PerformanceCounterCategory.Exists(CATEGORY_NAME))
            {
                PerformanceCounterCategory.Delete(CATEGORY_NAME);
            }
        }


        public DefaultInstrumentationProvider(string instanceName)
        {
            InitCounter(instanceName);
        }

        private void InitCounter(string instanceName)
        {
            _connectedCounter = new PerformanceCounter(CATEGORY_NAME, "Connection Created/sec", instanceName, false);

            _disconnetedCounter = new PerformanceCounter(CATEGORY_NAME, "Connection Disposed/sec", instanceName, false);

            _connectionCounter = new PerformanceCounter(CATEGORY_NAME, "Connection Actived", instanceName, false);
        }


        public void FireConnectionCreated()
        {
            _connectedCounter.Increment();
            _connectionCounter.Increment();
        }

        public void FireConnectionDisposed()
        {
            _disconnetedCounter.Increment();
            _connectionCounter.Decrement();
        }
 
    }
}
